// 定义全局过滤器 -- 设置保留位数
Vue.filter("format", function (val, type, num) {
    if (typeof (val) != 'number') {
        return 0.00;
    }
    return type + val.toFixed(num)
});

// 自定义全局指定 -- 设置字体的颜色
Vue.directive('color', {
    inserted(el, binding) {
        el.style.color = binding.value;
        el.style.fontWeight = "bold";
    }
});

// 创建Vue实例
let vm = new Vue({
    el: '#app',

    data: {
        // 商品列表 -- 用于显示页面中的数据
        productList: [],
        editProduct: {},
        isUpdate: false
    },
    methods: {
        // 修改数量
        changeCount(index, count) {
            let product = this.productList[index];
            //product.count = Number(product.count) + Number(count);

            let id = product.id;
            let c = Number(product.count) + Number(count);


            $.ajax("http://localhost:9999/pms-service/api/product_update_count.do", {
                type: 'put',
                data: { "pid": id, "pcount": c },
                dataType: 'json',
                success: function (res) {
                    if (res.code == 200) {
                        product.count = c;
                    }
                }
            });

        },
        // 删除商品
        del(pid) {

            let that = this;

            if (confirm("您真的要删除码？")) {

                $.ajax("http://localhost:9999/pms-service/api/product_delete.do", {
                    type: 'delete',
                    data: { "pid": pid },
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 200) {
                            that.productList = that.productList.filter((p) => {
                                return p.id != pid;
                            });
                        }
                    }
                });

            }
        },
        // 编辑商品
        edit(product) {
            // 地址引用不一样了！
            this.editProduct = Object.assign({}, product);
            this.isUpdate = true;
        },
        // 添加商品或修改商品
        save() {
            let that = this;

            console.log(that.editProduct);

            if (this.isUpdate) {
                // 修改操作
                $.ajax("http://localhost:9999/pms-service/api/product_update.do", {
                    type: 'put',
                    data: that.editProduct,
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 200) {
                            that.productList.some((p, i) => {
                                // 查询要删除的商品编号
                                if (p.id == that.editProduct.id) {
                                    // 修改数组元素  
                                    that.productList.splice(i, 1, Object.assign({}, that.editProduct));
                                    // 退出循环
                                    return true;
                                }
                            });
                            that.cls();
                        }

                    }
                });

            } else {
                // 添加操作
                $.ajax("http://localhost:9999/pms-service/api/product_add.do", {
                    type: 'post',
                    data: that.editProduct,
                    dataType: 'json',
                    success: function (res) {
                        console.log(res);
                        if(res.code == 200) {
                            that.productList.push(that.editProduct);
                            that.cls();
                        }
                    }
                });
            }
        },
        // 清空表单
        cls() {
            this.editProduct = {};
            this.isUpdate = false;

            // DOM操作
            this.$refs.pid.disabled = false;
            this.$refs.pid.focus();
        }
    },
    computed: {
        // 计算总价
        total() {
            return this.productList.reduce((s, p) => {
                s = s + p.price * p.count;
                return s;
            }, 0);
        }
    },
    mounted() {
        // 初始化数据 - ajax请求
        // this.productList = products;
        let that = this;

        $.ajax("http://localhost:9999/pms-service/api/product_list.do", {
            type: 'get',
            dataType: 'json',
            success: function (res) {
                // console.log(res);
                that.productList = res.data;
            }
        });



    }
});